﻿namespace Cos.Core.Module
{
    using System;
    using System.Collections.Generic;
    using System.Web.Mvc;

    using log4net;

    using Microsoft.Practices.Unity;

    public abstract class ModuleRegistrationBase : AreaRegistration
    {
        protected readonly ILog Logger;

        protected ModuleRegistrationBase()
        {
            Logger = LogManager.GetLogger(this.GetType());
        }

        public override string AreaName
        {
            get { return this.Name; }
        }

        public virtual bool IsUiModule
        {
            get { return false; }
        }

        public abstract string Name { get; }

        public abstract IEnumerable<Type> ControllerTypes { get; }

        public abstract void Init();

        public override void RegisterArea(AreaRegistrationContext context)
        {
            this.Init();

            var resolver = DependencyResolver.Current as UnityDependencyResolver;

            if (resolver != null)
            {
                foreach (var controllerType in this.ControllerTypes)
                {
                    resolver.Container.RegisterType(controllerType, controllerType);
                }
            }

            context.MapRoute(
                this.AreaName + "_Module", 
                this.AreaName + "/{controller}/{action}", 
                new { action = "Index" });
        }
    }
}
